package com.moonlight.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moonlight.service.ActCardResultService;

/**
 * 
 * @ClassName ActCardResultServiceImpl
 * @Description 测试类业务实现层
 * @author wuxiangyi
 * @date 2022年9月4日 下午8:19:10
 */
@Service
public class ActCardResultServiceImpl implements ActCardResultService {

	@Override
	public void doParse(String condition, JSONArray ruleArray, JSONArray groupArray , StringBuffer buffer) throws Exception {
		// TODO Auto-generated method stub
		for (int i = 0 ; i < ruleArray.size() ; i ++) {
			JSONObject object = ruleArray.getJSONObject(i);
			if (condition.equals("AND")) {
				if (i == 0) {
					buffer.append(object.getString("col") + " " + (object.getString("con").equals("equal") ? "=" : "<>") + " '" + object.getString("value") + "' ");
				} else {
					buffer.append("and " + object.getString("col") + " " + (object.getString("con").equals("equal") ? "=" : "<>") + " '" + object.getString("value") + "' ");
				}
			} else {
				if (i == 0) {
					buffer.append(object.getString("col") + " " + (object.getString("con").equals("equal") ? "=" : "<>") + " '" + object.getString("value") + "' ");
				} else {
					buffer.append("or " + object.getString("col") + " " + (object.getString("con").equals("equal") ? "=" : "<>") + " '" + object.getString("value") + "' ");
				}
			}
		}
		for (int i = 0 ; i < groupArray.size() ; i ++) {
			JSONObject object = groupArray.getJSONObject(i);
			if (condition.equals("AND")) {
				buffer.append("and (");
			} else {
				buffer.append("or (");
			}
			String currentCondition = object.getString("condition");
			JSONArray currentRuleArray = object.getJSONArray("rule");
			JSONArray currentGroupArray = object.getJSONArray("group");
			doParse(currentCondition, currentRuleArray, currentGroupArray, buffer);
			buffer.append(")");
		}
	}

}
